﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace Hangout.Client.Controls.User
{
    public partial class UserInfoControl : UserControl
    {


        

        public event EventHandler ProfilePictureClicked;
        public event EventHandler StatusTextclicked;
        public event EventHandler ScoreClicked;
        public event EventHandler NameClicked;

        public MeetupService.Meetup Hangout { get; set; }

        public UserInfoControl()
        {
            InitializeComponent();
        }

        private AccountService.UserProfile profile;

        public AccountService.UserProfile Profile
        {
            get
            {
                return profile;

            }
            set
            {
                try
                {
                    profile = value;
                    if (Profile != null)
                    {
                        LoadData();
                    }
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                }
            }
        }

        private void LoadData()
        {
            try
            {
                if (Profile != null)
                {
                    if (!String.IsNullOrWhiteSpace(Profile.User.Name))
                    {
                        NameLBL.Text = Profile.User.Name;
                    }
                    else
                    {
                        NameLBL.Text = "[NO NAME]";
                    }

                    GetLastHangout();
                    LoadMyProfilePicture();
                    LoadScore();
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void GetLastHangout()
        {
            try
            {
                
                Services.MeetupServiceClient.GetLastHangoutCompleted += new EventHandler<MeetupService.GetLastHangoutCompletedEventArgs>(client_GetLastHangoutCompleted);
                Services.MeetupServiceClient.GetLastHangoutAsync(Profile.User.UserID, Settings.Settings.FacebookAccessToken);
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void client_GetLastHangoutCompleted(object sender, MeetupService.GetLastHangoutCompletedEventArgs e)
        {
            Services.MeetupServiceClient.GetLastHangoutCompleted -= new EventHandler<MeetupService.GetLastHangoutCompletedEventArgs>(client_GetLastHangoutCompleted);
            try
            {

                if (e.Error == null)
                {
                    if (e.Result != null)
                    {
                        Hangout = e.Result;
                        StatusLBL.Text = e.Result.StatusText;
                    }
                    else
                    {
                        StatusLBL.Text = Profile.User.Name + " has joined Zoik it!";
                    }

                    ShowHangout.Begin();
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }


        }


        private void LoadScore()
        {

            try
            {
               
                Services.UserScoreServiceClient.GetLastScoreCompleted += new EventHandler<UserScoreService.GetLastScoreCompletedEventArgs>(client_GetLastScoreCompleted);
                Services.UserScoreServiceClient.GetLastScoreAsync(Profile.User.UserID, Settings.Settings.FacebookAccessToken);
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        void client_GetLastScoreCompleted(object sender, UserScoreService.GetLastScoreCompletedEventArgs e)
        {
            Services.UserScoreServiceClient.GetLastScoreCompleted -= new EventHandler<UserScoreService.GetLastScoreCompletedEventArgs>(client_GetLastScoreCompleted);
            try
            {
                if (e.Error == null)
                {
                    ScoreLBL.Text = e.Result.ToString();
                    ScorePG.Visibility = System.Windows.Visibility.Collapsed;
                    ShowScore.Begin();
                }
                else
                {
                    ScoreLBL.Text = "XX";
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void LoadMyProfilePicture()
        {
            try
            {
                if (!String.IsNullOrWhiteSpace(Profile.User.ProfilePicURL))
                {
                    MyProfilePicIMG.Source = new BitmapImage(new Uri(Profile.User.ProfilePicURL, UriKind.Absolute));
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void NameLBL_Tap(object sender, GestureEventArgs e)
        {
            try
            {

                if (NameClicked != null)
                {
                    NameClicked(null, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void StatusLBL_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                if (Hangout != null)
                {
                    if (StatusTextclicked != null)
                    {
                        StatusTextclicked(null, new EventArgs());
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void ScoreLBL_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                if (ScoreClicked != null)
                {
                    ScoreClicked(null, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void MyProfilePicIMG_Tap(object sender, GestureEventArgs e)
        {
            try
            {
                if (ProfilePictureClicked != null)
                {
                    ProfilePictureClicked(null, new EventArgs());
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }



    }
}
